//给定一个二叉树，检查它是否是镜像对称的。 
//
// 
//
// 例如，二叉树 [1,2,2,3,4,4,3] 是对称的。 
//
//     1
//   / \
//  2   2
// / \ / \
//3  4 4  3
// 
//
// 
//
// 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 
//
//     1
//   / \
//  2   2
//   \   \
//   3    3
// 
//
// 
//
// 进阶： 
//
// 你可以运用递归和迭代两种方法解决这个问题吗？ 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 1630 👎 0

package leetcode.editor.cn;


import java.util.LinkedList;
import java.util.Queue;

public class _101_SymmetricTree {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        int n = Integer.MAX_VALUE;
//        int[] nums = new int[]{1, 2, 2, 3, 4, 4, 3, n, n, n, n, n, n, n, n};
        int[] nums = new int[]{1, 2, 3, n, n, n, n};
        TreeNode root = creatTree(nums, 0);
        Solution solution = new _101_SymmetricTree().new Solution();
        System.out.println(solution.isSymmetric(root));
    }

    /**
     * 层序创建二叉树
     */
    private static TreeNode creatTree(int[] nums, int index) {
        if (nums[index] == Integer.MAX_VALUE) {
            return null;
        }
        TreeNode root = new TreeNode(nums[index]);
        root.left = creatTree(nums, 2 * index + 1);
        root.right = creatTree(nums, 2 * index + 2);
        return root;
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {

        /**
         * 官方解法，队列
         *
         * @param root
         * @return
         */
        public boolean isSymmetric(TreeNode root) {
            Queue<TreeNode> q = new LinkedList<TreeNode>();
            q.add(root);
            q.add(root);

            while (!q.isEmpty()) {
                TreeNode t1 = q.poll();
                TreeNode t2 = q.poll();
                if (t1 == null && t2 == null) {
                    continue;
                } else if (t1 == null || t2 == null) {
                    return false;
                } else if (t1.val != t2.val) {
                    return false;
                } else {
                    q.add(t1.left);
                    q.add(t2.right);
                    q.add(t1.right);
                    q.add(t2.left);
                }
            }
            return true;
        }

        /**
         * 官方解法，利用两个指针进行递归
         *
         * @param root
         * @return
         */
        public boolean isSymmetric_doublePoint(TreeNode root) {
            return check(root, root);
        }

        private boolean check(TreeNode p, TreeNode q) {
            if (p == null && q == null) {
                return true;
            } else if (p == null || q == null) {
                return false;
            } else {
                return (p.val == q.val) && check(p.left, q.right) && check(q.right, p.left);
            }
        }

        /**
         * 搞错了，搞成每个接节点对称了
         *
         * @param root
         * @return
         */
        public boolean isSymmetric_error(TreeNode root) {
            if (root.left == null && root.right == null) {
                return true;
            } else if ((root.left == null && root.right != null) || (root.left != null && root.right == null)) {
                return false;
            } else {
                return (root.left.val == root.right.val) && isSymmetric_error(root.left) && isSymmetric_error(root.right);
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}